package com.cy.oa.controller;

import java.io.IOException;
import java.util.List;
import java.util.zip.ZipInputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/deploy")
public class DeployController {
	@Resource
	RepositoryService repositoryService;

	@RequestMapping("/deployPage")
	public String deployPage() {
		return "oa/deploy/deployPage";
	}
	@RequestMapping("/toDeployListPage")
	public String toDeployListPage(Model model) {
		List<Deployment> resList = repositoryService.createDeploymentQuery().orderByDeploymenTime().desc().list();
		model.addAttribute("resList", resList);
		return "oa/deploy/deployListPage";
	}
	
	@RequestMapping("/doDeploy")
	public String deploy(HttpServletRequest request,MultipartFile processFile,Model model) {
		String processName = request.getParameter("processName");
		if(null!=processFile) {
			String fileName = processFile.getOriginalFilename();
			if(fileName.contains(".zip")) {
				try {
					Deployment depoly=repositoryService.createDeployment()
					.addZipInputStream(new ZipInputStream(processFile.getInputStream()))
					.name(processName).deploy();
					model.addAttribute("msg","流程上传成功！！！");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}else {
				model.addAttribute("msg","上传文件不是zip压缩文件");
			}	
		}else {
			model.addAttribute("msg","上传文件为空！！！");
		}
		return "oa/deploy/deployPage";
	}
}
